package com.inoneone.youba.mcht.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inoneone.toolkit.SnowFlake;
import com.inoneone.youba.mcht.dao.mapper.ExtendSysEventMapper;
import com.inoneone.youba.mcht.dao.mapper.MerchantMapper;
import com.inoneone.youba.mcht.dao.mapper.SysEventMapper;
import com.inoneone.youba.mcht.dao.po.*;
import com.inoneone.youba.mcht.dto.PageRsp;
import com.inoneone.youba.mcht.dto.req.MyEventReq;
import com.inoneone.youba.mcht.dto.rsp.MyEventRsp;
import com.inoneone.youba.mcht.enums.ErrorCode;
import com.inoneone.toolkit.exception.BusinessRuntimeException;
import com.inoneone.youba.mcht.pojo.Event;
import com.inoneone.youba.mcht.service.SafeApi;
import com.inoneone.youba.mcht.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class SafeApiImpl implements SafeApi {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SnowFlake snowFlake;

    @Autowired
    ExtendSysEventMapper extendSysEventMapper;

    @Autowired
    MerchantMapper merchantMapper;

    @Autowired
    SysEventMapper sysEventMapper;

    /**
     * Description: 查询系统事件</br>
     * Date: 2017-06-28 14:01:29</br>
     *
     * @param myEventReq@return
     * @author 姬远玄
     * @since JDK 1.7
     */
    @Override
    public PageRsp<MyEventRsp> querySysEvent(MyEventReq myEventReq, String mchId) {
        ExtendSysEventExample extendSysEventExample = new ExtendSysEventExample();
        extendSysEventExample.setMchId(mchId);
        if(myEventReq.getBeginTime() != 0)
        {
            extendSysEventExample.setBeginTime(new Date(myEventReq.getBeginTime()));

        }
        if(myEventReq.getEndTime() != 0)
        {
            extendSysEventExample.setEndTime(new Date(myEventReq.getEndTime()));
        }
        if(!CommonUtil.isNullOrEmpty(myEventReq.getKeywords()))
        {
            extendSysEventExample.setKeywords(myEventReq.getKeywords());
        }

        if(myEventReq.getType() != null)
        {
            extendSysEventExample.setType(myEventReq.getType());
        }

        //分页查询
        PageHelper.startPage(myEventReq.getPageNum(), myEventReq.getPageSize());
        List<SysEvent> sysEvents = extendSysEventMapper.selectByExample(extendSysEventExample);
        PageInfo<SysEvent> pageCards = new PageInfo<>(sysEvents);

        //转换输出类
        List<MyEventRsp> myEventRspList = new ArrayList<>();
        for (SysEvent sysEvent : sysEvents)
        {
            MyEventRsp myEventRsp = new MyEventRsp();
            BeanUtils.copyProperties(sysEvent, myEventRsp);
            myEventRspList.add(myEventRsp);
        }
        PageRsp<MyEventRsp> pageMyEventRsp = new PageRsp<>(pageCards.getTotal(), pageCards.getPageSize(), pageCards.getPageNum(), myEventRspList);

        return pageMyEventRsp;
    }

    @Override
    public void modifyPassword(String password, String mchId) {

        Merchant merchant = new Merchant();
        merchant.setId(mchId);
        merchant.setPassword(password);
        int result = merchantMapper.updateByPrimaryKeySelective(merchant);
        if(result != 1)
        {
            throw  new BusinessRuntimeException(ErrorCode.ERR_UPDATE);
        }
    }

    @Override
    public void modifyPayPassword(String password, String mchId) {

        Merchant merchant = new Merchant();
        merchant.setId(mchId);
        merchant.setPayPassword(password);
        int result = merchantMapper.updateByPrimaryKeySelective(merchant);
        if(result != 1)
        {
            throw  new BusinessRuntimeException(ErrorCode.ERR_UPDATE);
        }
    }

    @Override
    @Async
    public void log(Event event)
    {
        SysEvent sysEvent = new SysEvent();
        sysEvent.setId(snowFlake.nextId());
        sysEvent.setCardId(event.getCardId());
        sysEvent.setCardName(event.getCardName());
        sysEvent.setMchId(event.getMchId());
        sysEvent.setTankerId(event.getTankerId());
        sysEvent.setTankerName(event.getTankerName());
        sysEvent.setMchName(event.getMchName());
        sysEvent.setDescription(event.getDesc());
        sysEvent.setType(event.getType());
        sysEvent.setHappenTime(new Date());

        sysEventMapper.insertSelective(sysEvent);
        logger.debug("记录系统日志");
    }
}
